package com.core.service;

import com.core.bean.Receiving;
import com.core.bean.Shipments;
import com.core.bean.ShoppingOrder;
import com.core.dao.*;
import com.core.dao.mybatismysql.ReceivingDao;

import java.util.Date;
public class ShoppingService {


    public static final int STEP_NEW = 1;
    public static final int STEP_PAYED = 2;
    public static final int STEP_DELIVER= 3;
    public static final int STEP_RECEIVER = 4;
    public static final int STEP_COMMENTED = 5;

    private String id;
    private ShoppingOrder shoppingOrder;
    private Shipments shipments;
    private Receiving receiving;
    private Date time = new Date();
    private int step;

    public void downOrder(){
        shoppingOrder = new ShoppingOrder();
        shipments = new Shipments();
        receiving = new Receiving();
        IShoppingOrderDao shoppingOrderDao = AbstractDaoFactory.create(AbstractDaoFactory.MYBATIS_MYSQL_DAO).getShoppingOrderDao();
        IShipmentsDao shipmentsDao = AbstractDaoFactory.create(AbstractDaoFactory.JDBC_MYSQL_DAO).getShipmentsDao();
        IReceivingDao receivingDao = AbstractDaoFactory.create(AbstractDaoFactory.MYBATIS_MYSQL_DAO).getReceivingDao();
        ITransaction transaction = AbstractDaoFactory.create(AbstractDaoFactory.JDBC_MYSQL_DAO).getTransaction();
        try{
            transaction.open();
            shoppingOrderDao.add(shoppingOrder);
            shipmentsDao.add(shipments);
            receivingDao.add(receiving);
            transaction.commit();
        }catch (Exception e){
            transaction.rollback();
        }finally {
            transaction.close();
        }
        setStep(STEP_NEW);
    }
    public void pay(){
        setStep(STEP_PAYED);
        //if()
        //健全性
        shoppingOrder.setStatus(1);
    }
    public void deliver(){
        setStep(STEP_DELIVER);
        receiving.setStatus(1);
    }
    public void receiver(){
        setStep(STEP_RECEIVER);
        receiving.setStatus(1);
    }

    public void setStep(int step){
        this.step = step;
    }
}
